Ext.ns("crs.grid");

crs.grid.MyRegisterationGridPanel = Ext.extend(Ext.grid.EditorGridPanel, {

	/* begin of fields initialization */
	height : 400,
	default_url : 'review_copyrightRegs.do',
	search_url : 'review_regSearch.do',
	defaultProxy : '',
	searchProxy : '',
	pageSize : 15,
	closable : true,
	params : '',
	root : 'listCopyrightRegisteration',
	totalProperty : 'pagination.totalCount',
	title : '著作权登记列表',
	plugins : [new Ext.ux.grid.RowEditor({
				listeners : {
					'afteredit' : function(roweditor, changes, record, rowIndex) {
						var dataToSend = {};
						dataToSend.copyrightRegisteration = record.data;
						dataToSend = Ext.encode(dataToSend);
						Ext.Ajax.request({
									url : "review_updateCopyrightRegist.do",
									jsonData :dataToSend,
									success : function(resp) {
										var rp = Ext.decode(resp.responseText);
										roweditor.grid.el.unmask();
										if (rp.success == true) {
											Ext.MessageBox.alert('提示', '更新成功!');
										}else{
											Ext.MessageBox.alert('异常', '更新出现异常!');
										}
										roweditor.grid.store.load({
											params : {
												start : 0,
												limit : 15,
												asc : false,
												orderByField : 'registDate'
											}
										});
									},
									scope : this
								});
					},
					scope : this
				}
			})],
	viewConfig : {
		deferEmptyText : false,
		emptyText : '没有可显示数据',
		enableTextSelection : true,
		forceFit : true
	},
	defaultProxy : {},
	fields : ['id', 'serialNum', 'copyrighter', 'productName', 'contact',
			'phoneNum', 'email', 'dateOfDealedByOrg', 'registDate', 'mobileNum'],
	columns : [{
				header : '流水号',
				dataIndex : 'serialNum'
			}, {
				header : '著作权人',
				dataIndex : 'copyrighter'
			}, {
				header : '软件名称',
				dataIndex : 'productName'
			}, {
				header : '联系人',
				dataIndex : 'contact',
				editor : new Ext.form.TextField({
							allowBlank : true
						})
			}, {
				header : '电话',
				dataIndex : 'phoneNum',
				editor : new Ext.form.TextField({
					allowBlank : true
				})
			}, {
				header : '手机',
				dataIndex : 'mobileNum',
				editor : new Ext.form.TextField({
					allowBlank : true
				})
			}, {
				header : '邮箱',
				dataIndex : 'email',
				editor : new Ext.form.TextField({
					allowBlank : true
				})
			}, {
				header : '登记日期',
				dataIndex : 'registDate'
			}, {
				width : 100,
				dataIndex : 'id',
				hidden : true
			}],

	sm : new Ext.grid.RowSelectionModel({
				singleSelect : true
			}),
	initComponent : function() {
		this.buildProxies();
		this.store = this.buildStore();
		// this.columns = this.buildColumns();
		this.bbar = this.buildBbar();
		this.tbar = this.buildTbar();
		crs.grid.MyRegisterationGridPanel.superclass.initComponent.call(this);
		this.on({

					'keydown' : function(event) {
						if (event.ctrlKey && event.getKey() == 67) {
							if (this.selModel.getSelections().length < 1) {

								return;
							}

							var record = this.selModel.getSelections()[0];

							window.clipboardData.setData("Text",
									record.data.serialNum + "\t"
											+ record.data.copyrighter + "\t"
											+ record.data.productName + "\t"
											+ record.data.contact + "\t"
											+ record.data.phoneNum + "\t"
											+ record.data.mobileNum + "\t"
											+ record.data.email + "\t"
											+ record.data.registDate);
							alert('已复制至粘贴板!');
						}
					},
					scope : this
				});

		this.store.load({
					params : {
						start : 0,
						limit : this.pageSize,
						asc : false,
						orderByField : 'registDate'
					}
				});
	},
	/* end of components appending to this panel */
	buildStore : function() {

		return new Ext.data.Store({
					proxy : this.defaultProxy,
					reader : new Ext.data.JsonReader({
								root : this.root,
								totalProperty : this.totalProperty,
								fields : this.fields
							})
				});

	},

	buildProxies : function() {

		this.defaultProxy = new Ext.data.HttpProxy({
					url : this.default_url,
					method : 'post'
				});

		this.searchProxy = new Ext.data.HttpProxy({
					url : this.search_url,
					method : 'post'
				});
	},
	buildTbar : function() {
		return new Ext.Toolbar({
			items : ['-', {
				xtype : 'button',
				text : '同步数据',
				handler : function() {
					Ext.Ajax.request({
								url : "review_syncRegistData.do",
								success : function(resp) {
									var rp = Ext.decode(resp.responseText);
									if (rp.success == true) {
										Ext.MessageBox.alert('提示', '同步成功!');
										this.store.load({
													params : {
														start : 0,
														limit : this.pageSize,
														asc : false,
														orderByField : 'registDate'
													}
												});
									} else if (rp.success == false) {
										Ext.MessageBox.alert('错误', '同步出现异常:'
														+ rp.message);
									}
								},
								scope : this

							});
				}.createDelegate(this)
			}, '->', {
				xtype : 'textfield',
				id : 'companyNameSearch'
			}, {
				xtype : 'button',
				text : '查询',
				handler : function(btn) {
					var searchValue = Ext.getCmp("companyNameSearch")
							.getValue();
					if (searchValue.trim().length == 0)
						return;
					this.clearStore();
					this.store.proxy = this.searchProxy;

					Ext.apply(this.store.baseParams, {
								"copyrightRegisteration.copyrighter" : searchValue
							});
					this.store.load({
								params : {
									start : 0,
									limit : this.pageSize
								}
							});
				}.createDelegate(this)

			}, '-']
		});
	},
	buildBbar : function() {
		return new Ext.PagingToolbar({
					store : this.store,
					pageSize : this.pageSize,
					displayInfo : true,
					listeners : {
						'beforechange' : function(cmp) {
							this.clearStore();
							this.store.proxy = this.defaultProxy;
						},
						scope : this

					}
				});
	},
	initStore : function() {
		this.store.removeAll();
		this.store.baseParams = {
			asc : false,
			orderByField : 'registDate'
		};
		this.store.params = {};
	},
	clearStore : function() {
		this.store.removeAll();
		this.store.baseParams = {};
		this.store.params = {};
	}

		/* end of handler implementation */
});

Ext.reg("myregisteration_grid", crs.grid.MyRegisterationGridPanel);